package com.heima.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.sensitive.dtos.SensitiveDTO;
import com.heima.model.sensitive.pojos.AdSensitive;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {

    @Autowired
    SensitiveMapper sensitiveMapper;


    @Override
    public ResponseResult queryDataByName(SensitiveDTO dto) {

        //判断前端传入参数name
        if (dto == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "参数有问题");
        }
        //检查参数page 和size
        dto.checkParam();

        //分页
        Page<AdSensitive> page1 = new Page<>(dto.getPage(), dto.getSize());

        //wrapper
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.<AdSensitive>lambdaQuery();
        if (StringUtils.isNotBlank(dto.getSensitives())) {
            wrapper.like(AdSensitive::getSensitives, dto.getSensitives());
        }

        IPage<AdSensitive> result = page(page1, wrapper);

        return new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal(), result.getRecords());
    }


    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.检查参数
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2. 判断是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //3.保存
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.检查参数
        if (adSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 如果名称修改 ， 检查是否存在

        //2.修改
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //1.检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询敏感词是否存在
        AdSensitive adSensitive = getById(id);
        if (adSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(sensitiveMapper.findAllSensitives());
    }
}
